<?php defined('SYSPATH') or die(); ?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>思途CMS{$coreVersion}</title>
    {template 'stourtravel/public/public_min_js'}
    {php echo Common::getCss('style.css,base.css,base_new.css,equity.css'); }
    {php echo Common::getScript("uploadify/jquery.uploadify.min.js,product_add.js,choose.js,st_validate.js,jquery.colorpicker.js,jquery.jqtransform.js,imageup.js,jqueryui/jquery-ui.min.js"); }
</head>
<body>
<!--顶部-->
<table class="content-tab">
    <tr>
        <td width="119px" class="content-lt-td" valign="top">
            {template 'stourtravel/public/leftnav'}
            <!--右侧内容区-->
        </td>
        <td valign="top" class="content-rt-td" style="overflow:auto;">
            <div class="cfg-header-bar">
                <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" onclick="window.location.reload()">刷新</a>
            </div>
            <div class="product-add-wrap pt-20 pl-20">
                <div class="product-tips">通用权益</div>
                <ul class="equity-list ty-equity-list clearfix" style="width: 1400px">
                    {loop $list['ty_list'] $v}
                    <li data-id="{$v['id']}" class="mr-10 equity-li">
                        <a href="javascript:;" class="equity-item">
                            <img class="icon" src="{$v['icon']}">
                            <div class="text">{$v['title']}</div>
                            <div class="isShow">
                                <label class="check-label">
                                    <input onclick="update_ishidden({$v['id']},$(this))" name="ishidden" type="checkbox" {if !$v['ishidden']}checked{/if} value="{$v['ishidden']}">显示
                                </label>
                            </div>
                        </a>
                    </li>
                    {/loop}
                </ul>
            </div>
            <div class="product-add-wrap pt-20 pl-20">
                <div class="product-tips">
                    自定义权益（点击图标可上传更换图标）
                    <input class="btn radius btn-secondary size-S" onclick="add_equity()" type="button" value="添加权益">
                </div>
                <ul class="equity-list set-equity-list clearfix" style="width: 1400px">
                    {loop $list['set_list'] $v}
                    <li data-id="{$v['id']}" class="mr-10">
                        <a href="javascript:;" class="equity-item">
                            <img onclick="update_icon({$v['id']},$(this))" class="icon" src="{$v['icon']}">
                            <div class="text">
                                <input type="text" onchange="update_title({$v['id']},$(this))" class="input-text w150" value="{$v['title']}" style="border: 0">
                            </div>
                            <div class="close" onclick="delete_icon({$v['id']},$(this))">×</div>
                            <div class="isShow">
                                <label class="check-label">
                                    <input onclick="update_ishidden({$v['id']},$(this))" name="ishidden" type="checkbox" {if !$v['ishidden']}checked{/if} value="{$v['ishidden']}">显示
                                </label>
                            </div>
                        </a>
                    </li>
                    {/loop}
                </ul>
            </div>
        </td>
    </tr>
</table>
<script>
    $( ".ty-equity-list" ).sortable({
        update : function(){
            var id = [];
            $(".ty-equity-list li").each(function(){
                id.push($(this).attr('data-id'));
            })
            $.ajax({
                url:SITEURL+'equity/ajax_equity_sort',
                data:{id:id},
                type:'post',
                dataType:'json',
            })
        }
    });
    $( ".set-equity-list" ).sortable({
        update : function(){
            var id = [];
            $(".set-equity-list li").each(function(){
                id.push($(this).attr('data-id'));
            })
            $.ajax({
                url:SITEURL+'equity/ajax_equity_sort',
                data:{id:id},
                type:'post',
                dataType:'json',
            })
        }
    });

    function delete_icon(id,obj)
    {
        $.ajax({
            url:SITEURL+'equity/ajax_delete_equity',
            data:{id:id},
            type:'post',
            dataType:'json',
            success:function(data) {
                if(data.status){
                    obj.parent().parent().remove();
                }else{
                    ST.Util.showMsg('删除失败!',5);
                }
            }
        })
    }

    function update_ishidden(id,obj)
    {
        var ishidden = obj.val();
        $.ajax({
            url:SITEURL+'equity/ajax_update_equity',
            data:{id:id, ishidden:ishidden},
            type:'post',
            dataType:'json',
            success:function(data) {
            }
        })
    }

    function update_title(id,obj)
    {
        var title = obj.val();
        $.ajax({
            url:SITEURL+'equity/ajax_update_equity',
            data:{id:id, title:title},
            type:'post',
            dataType:'json',
            success:function(data) {
            }
        })
    }

    function update_icon(id,obj)
    {
        ST.Util.showBox('上传图标', SITEURL + 'image/insert_view', 0,0, null, null, document, {loadWindow: window, loadCallback: Insert});
        function Insert(result,bool){
            console.log(result);
            var temp =result.data[0].split('$$');
            obj.attr('src',temp[0]);
            $.ajax({
                url:SITEURL+'equity/ajax_update_equity',
                data:{id:id, icon:temp[0]},
                type:'post',
                dataType:'json',
                success:function(data) {
                    if(data.status){
                        ST.Util.showMsg('更换成功！',4);
                    }
                }
            })
        }
    }

    function add_equity()
    {
        var url = SITEURL+'equity/dialog_add_equity';
        CHOOSE.setSome(
            "选择权益",
            {height:500,width:720,loadWindow:window,loadCallback:callback},
            url,
            1
        )
    }

    function callback(res,status)
    {
        $.ajax({
            url:SITEURL+'equity/ajax_save_equity',
            data:res,
            type:'post',
            dataType:'json',
            success:function(data) {
                if(data.status){
                    window.location.reload();
                }
            }
        });
    }
</script>
</body>
</html>